<template>
  <div class="login">
    <div class="login-logo hidden-md-and-down">
      <span> 威廉英国房源 | 通行证</span>
    </div>
    <h2 class="main-title"><span>威廉英国房源</span></h2>
    <h3 class="sub-title">极简设计 · 智慧租务 · 英伦格调</h3>

    <div class="login-box">
      <div class="title">密码登录</div>

      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
            <svg-icon
              slot="prefix"
              icon-class="user"
              class="el-input__icon input-icon"
              style="color: #e6e6e6"
            />
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="password"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>

        <el-form-item prop="code" v-if="captchaOnOff">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="validCode"
              class="el-input__icon input-icon"
            />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>

        <el-checkbox
          v-model="loginForm.rememberMe"
          style="margin: 0 0 25px 0"
        >记住密码</el-checkbox>

        <el-form-item style="width: 100%">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width: 100%"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>

          <div style="float: right" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </el-form-item>
      </el-form>
    </div>

    <!--  底部  -->
    <div class="el-login-footer">
      <span>
        Copyright © 2021-{{ new Date().getFullYear() }} 威廉英国房源 William
        UK Estates. All Rights Reserved.
      </span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import "element-ui/lib/theme-chalk/display.css";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "demo",
        password: "123456",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
        password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
        code: [{ required: false, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关（由后端 /captchaImage 决定；你也可以直接在后端关闭）
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        code: "",
        uuid: this.loginForm.uuid,
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (!valid) return;
        this.loading = true;

        if (this.loginForm.rememberMe) {
          Cookies.set("username", this.loginForm.username, { expires: 30 });
          Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
          Cookies.set("rememberMe", this.loginForm.rememberMe, { expires: 30 });
        } else {
          Cookies.remove("username");
          Cookies.remove("password");
          Cookies.remove("rememberMe");
        }

        this.$store
          .dispatch("Login", this.loginForm)
          .then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(() => {});
          })
          .catch(() => {
            this.loading = false;
            if (this.captchaOnOff) this.getCode();
          });
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  align-items: center;
  flex-direction: column;
  height: 100%;
  background: radial-gradient(circle at 20% -10%, #fefefe 0%, #f5f7fb 45%, #e3eaf5 100%);
  position: relative;
  color: #1f2937;
}
.login-logo {
  width: 260px;
  height: 32px;
  position: absolute;
  left: 120px;
  top: 60px;
  padding-right: 10px;
  border-radius: 36px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(15, 23, 42, 0.08);
  box-shadow: 0 12px 24px rgba(15, 23, 42, 0.08);
  span {
    line-height: 32px;
    font-size: 18px;
    padding-left: 16px;
    color: rgba(15, 23, 42, 0.82);
    font-weight: 500;
  }
}
.main-title {
  margin: 120px 0 0 0;
  height: 48px;
  color: #0f172a;
  span {
    line-height: 48px;
    font-size: 36px;
    padding: 0 24px;
    color: #0f172a;
    font-weight: 600;
    letter-spacing: 2px;
  }
}
.sub-title {
  margin: 20px 0 70px 0;
  text-align: center;
  color: rgba(55, 65, 81, 0.85);
  font-size: 18px;
  letter-spacing: 0.12em;
}

.login-box {
  border-radius: 28px;
  width: 420px;
  background: rgba(255, 255, 255, 0.98);
  margin: 0 auto;
  padding: 30px 0 0 0;
  box-shadow: 0 40px 70px rgba(15, 23, 42, 0.15);
  border: 1px solid rgba(148, 163, 184, 0.18);
  .title {
    padding-left: 32px;
    font-size: 20px;
    color: #0f172a;
  }
}

.login-form {
  border-radius: 28px;
  background: transparent;
  width: 420px;
  padding: 40px 40px 20px 40px;
  .el-input {
    height: 42px;
    input {
      height: 42px;
      background: rgba(248, 250, 252, 0.9);
      border: 1px solid rgba(148, 163, 184, 0.32);
      color: #0f172a;
      border-radius: 14px;
      transition: border 0.3s ease, box-shadow 0.3s ease;
      &::placeholder {
        color: rgba(100, 116, 139, 0.7);
      }
      &:focus {
        border-color: rgba(10, 132, 255, 0.55);
        box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.12);
        background: rgba(255, 255, 255, 1);
      }
    }
  }
  .input-icon {
    height: 42px;
    width: 14px;
    margin-left: 2px;
    color: rgba(148, 163, 184, 0.8);
  }
  .el-checkbox {
    color: rgba(55, 65, 81, 0.78);
  }
  .el-button--primary {
    background: linear-gradient(135deg, #0a84ff 0%, #5ac8fa 100%);
    border: none;
    border-radius: 16px;
    box-shadow: 0 18px 36px rgba(10, 132, 255, 0.26);
    font-weight: 600;
    letter-spacing: 0.28em;
    color: #fff;
  }
  .el-button--primary:hover,
  .el-button--primary:focus {
    background: linear-gradient(135deg, #5ac8fa 0%, #0a84ff 100%);
    box-shadow: 0 20px 38px rgba(90, 200, 250, 0.28);
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: rgba(100, 116, 139, 0.8);
}
.login-code {
  width: 33%;
  height: 42px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
    border-radius: 12px;
    border: 1px solid rgba(148, 163, 184, 0.3);
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: rgba(55, 65, 81, 0.65);
  font-family: "SF Pro Text", -apple-system, BlinkMacSystemFont, "Segoe UI",
    Arial, sans-serif;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 42px;
}
</style>
